package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    //新增套餐
    @PostMapping
    @CacheEvict(value = "setmealCache", key = "setmealDto.categoryId")
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);

        return R.success("保存成功");
    }

    //分页查询套餐
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //创建分页构造
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //拼接条件
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //执行分页查询
        setmealService.page(pageInfo, wrapper);
        //创建SetmealDto分页构造器
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //将除了records以外的所有属性都赋值给Dto
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        //转换records属性
        List<SetmealDto> setmealDtos = new ArrayList<>();
        //这是套餐的基本信息
        List<Setmeal> setmeals = pageInfo.getRecords();
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            //将遍历出来的所有基本属性封装到Dto中
            BeanUtils.copyProperties(setmeal, setmealDto);
            //根据category的id查询到套餐数据
            Category category = categoryService.getById(setmealDto.getCategoryId());
            //将套餐分类的名字写入到 Dto中
            setmealDto.setCategoryName(category.getName());
            //将所有数据写入到集合
            setmealDtos.add(setmealDto);
        }
        //把封装好Dto信息传给分页查询
        setmealDtoPage.setRecords(setmealDtos);
        //分好页的Dto对象封装到R中 完成返回
        return R.success(setmealDtoPage);
    }


    //套餐的删除操作
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(Long[] ids) {

        setmealService.removeByIdWithDish(ids);
        return R.success("删除成功");
    }

    //套餐的起售停售操作
    @PostMapping("/status/{sta}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> status(@PathVariable Integer sta, Long[] ids) {

        for (Long id : ids) {
            LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
            //set status = ?
            updateWrapper.set(Setmeal::getStatus, sta);
            //where id = ?
            updateWrapper.eq(Setmeal::getId, id);
            //调用update方法
            setmealService.update(updateWrapper);
        }
        return R.success("修改状态成功");

    }

    //商品回显
    @GetMapping("/{id}")
    public R<SetmealDto> upDate(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.showById(id);
        return R.success(setmealDto);
    }

    //商品修改实现
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> upDate(@RequestBody SetmealDto setmealDto) {

        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }

    //手机端 套餐展示
    @GetMapping("/list")
    //value = 组的名称(缓存名称) key = 存数据的键
    @Cacheable(value = "setmealCache", key = "setmeal.categoryId")
    public R<List<Setmeal>> showByPhone(Setmeal setmeal) {
        //根据id查
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //拼接条件
        wrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        //and 状态 =1
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        return R.success(setmealService.list(wrapper));


    }
}
